package vossenenkonijnen;
import java.util.List;
import java.util.Iterator;
import java.util.Random;

import controller.Field;
import controller.Randomizer;

/**
 * A simple model of a hunter.
 * Hunters age, move, eat rabbits, and die.
 * 
 * @author Projectgroep 5
 * @version 2008.03.30
 */
public class Hunter implements Actor
{
    // Characteristics shared by all hunters (static fields).
    
    // A shared random number generator to control breeding.
    private static final Random rand = Randomizer.getRandom();
    
    // Individual characteristics (instance fields).
    String hunter = "Hunter";
    
    private boolean active;
    private Field field;
    private Location location;

    /**
     * Create a hunter. A hunter can be created as a new born (age zero
     * and not hungry) or with a random age and food level.
	 *
     * @param field The field currently occupied.
     * @param location The location within the field.
     */
    public Hunter(Field field, Location location)
    {
    	active = true;
        this.field = field;
        setLocation(location);
    }
    
    /**
     * This is what the hunter does most of the time: it hunts for
     * rabbits. In the process, it might breed, die of hunger,
     * or die of old age.
     * @param newHunters A list to add hunter to.
     */
    public void act(List<Actor> newHunters)
    {
        if(isActive()) {
            // Move towards a source of animal if found.
            Location location = getLocation();
            Location newLocation = findFood(location);
            if(newLocation == null) { 
                // No animal found - try to move to a free location.
                newLocation = getField().freeAdjacentLocation(location);
            }
            // See if it was possible to move.
            if(newLocation != null) {
                setLocation(newLocation);
            }
            
        }
    }
    
    /**
     * Tell the hunter to look for rabbits adjacent to its current location.
     * Only the first live rabbit is eaten.
     * @param location Where in the field it is located.
     * @return Where food was found, or null if it wasn't.
     */
    private Location findFood(Location location)
    {
        Field field = getField();
        List<Location> adjacent = field.adjacentLocations(getLocation());
        Iterator<Location> it = adjacent.iterator();
        while(it.hasNext()) {
            Location where = it.next();
            Object animal = field.getObjectAt(where);
            if(animal instanceof Rabbit) {
                Rabbit rabbit = (Rabbit) animal;
                if(rabbit.isAlive()) { 
                    rabbit.setDead();
                    // Remove the dead rabbit from the field.
                    return where;
                }
            }
            else if(animal instanceof Fox)
            {
            	Fox fox = (Fox) animal;
            	if(fox.isAlive())
            	{
            		fox.setDead();
            		//Remove the dead fox from the field.
            		return where;
            	}
            }
            else if(animal instanceof Wolf)
            {
            	Wolf wolf = (Wolf) animal;
            	if(wolf.isAlive())
            	{
            		wolf.setDead();
            		// Remove the dead wolf from the field.
            		return where;
            	}
            }

        }
        return null;
    }
    
    /**
     * Indicate that the animal is no longer alive.
     * It is removed from the field.
     */
    public void setDead()
    {
        active = false;
        if(location != null) {
            field.clear(location);
            location = null;
            field = null;
        }
    }
    
    public boolean isActive()
    {
    	return active;
    }
    
    /**
     * Place the animal at the new location in the given field.
     * @param newLocation The animal's new location.
     */
    public void setLocation(Location newLocation)
    {
        if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }
    
    /**
     * Return the animal's location.
     * @return The animal's location.
     */
    public Location getLocation()
    {
        return location;
    }
    
    /**
     * Return the animal's field.
     * @return The animal's field.
     */
    public Field getField()
    {
        return field;
    }
    
    public String getName() {
    	return hunter;
    }
}

